package Algorithm.prim;

import java.util.Arrays;

/**
 * 普利姆算法
 */
public class PrimAlgorithm {
    public static void main(String[] args) {

        char[] data = new char[]{'A','B','C','D','E','F','G'};
        int verxs = data.length;

        //邻接矩阵使用二维数组表示
        int[][] weight = new int[][]{
                {10000,5,7,10000,10000,10000,2},
                {5,10000,10000,9,10000,10000,3},
                {7,10000,10000,10000,8,10000,10000},
                {10000,9,10000,10000,10000,4,10000},
                {10000,10000,8,10000,10000,5,4},
                {10000,10000,10000,4,5,10000,6},
                {2,3,10000,10000,4,6,10000},
        };

        MGraph mGraph = new MGraph(verxs);
        MinTree minTree = new MinTree();
        minTree.creatGraph(mGraph,verxs,data,weight);
        minTree.showGraph(mGraph);

        minTree.prim(mGraph,0);


    }
}

//创建最小生成树
class MinTree{

    //创建图的邻接矩阵
    /**
     *
     * @param graph  图对象
     * @param verxs 图对应的顶点个数
     * @param data 图的各个顶点的值
     * @param weight 图的邻接矩阵
     */
    public void creatGraph(MGraph graph,int verxs,char[] data ,int[][] weight){

        for (int i = 0; i <verxs ; i++) {
            graph.data[i] = data[i];
            for (int j = 0; j <verxs ; j++) {
                graph.weight[i][j] = weight[i][j];
            }
        }
    }

    //显示图的方法
    public void showGraph(MGraph graph){
        for (int[] link : graph.weight) {
            System.out.println(Arrays.toString(link));
        }
    }

    //编写prim算法。得到最小生成树
    /**
     *
     * @param graph 图
     * @param v 表示从图的第几个顶点开始生成'A'->0, 'B'->1.....
     */
    public void prim(MGraph graph, int v){

        //用来标记节点是否被访问过，默认都为0代表都没有访问过
        int[] visited = new int[graph.verxs];
        for (int i = 0; i <graph.verxs ; i++) {
            visited[i] = 0;
        }

        //把当前这个节点标记访问1代表访问
        visited[v] = 1;

        //用h1和h2记录两个顶点的下标
        int h1 = -1;
        int h2 = -1;
        int minWeight = 10000;  //将minWeight初始化一个较大的数
        int sum = 0;  //记录路径长度

        for (int k = 1; k < graph.verxs; k++) { //n个顶点生成n-1条边，所以k从1开始

            //这个是确定每一次生成的子图和那个节点的距离最近
            for (int i = 0; i < graph.verxs; i++) {  //i节点表示被访问过的节点
                for (int j = 0; j < graph.verxs; j++) { //j节点表示还没有访问过的节点
                    //满足条件：当前节点已经被访问，下一个节点没有被访问，两个节点直接连通
                    if (visited[i] == 1 && visited[j] == 0 && graph.weight[i][j] < minWeight) {
                        //替换minWight
                        minWeight = graph.weight[i][j];
                        //记录两个直接连通的节点的路径的二维数组的坐标
                        h1 = i;
                        h2 = j;
                    }
                }
            }

            //for循环结束后找到一条最小的边
            System.out.println("边<" + graph.data[h1] + "," + graph.data[h2] + "> 权值:" + minWeight);
            sum = sum + minWeight;

            //将当前节点标记为已经访问
            visited[h2] = 1;

            //需要重置minWeight
            minWeight = 10000;
        }
        System.out.println("最短路径长度为："+sum);
    }
}

class MGraph{
    int verxs;  //表示图的节点个数
    char[] data;  //表示节点数据
    int[][] weight; //存放边，邻接矩阵

    public MGraph(int verxs) {
        this.verxs = verxs;
        data = new char[verxs];
        weight = new int[verxs][verxs];
    }
}
